package com.hm.manage.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.repository.AbstractRepository;
import com.hm.common.utils.DictUtils;
import com.hm.common.utils.StringUtils;
import com.hm.common.utils.uuid.UUID;
import com.hm.manage.dto.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Service;
import com.hm.manage.mapper.LocalpicMapper;
import com.hm.manage.domain.Localpic;
import com.hm.manage.service.ILocalpicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 本地图片Service业务层处理
 * 
 * @author hm
 * @date 2025-03-24
 */
@Slf4j
@Service
public class LocalpicServiceImpl extends ServiceImpl<LocalpicMapper,Localpic> implements ILocalpicService
{
    @Autowired
    private LocalpicMapper localpicMapper;

    @Resource
    private KafkaTemplate<String,Object> kafkaTemplate;

    /**
     * 查询本地图片
     * 
     * @param id 本地图片主键
     * @return 本地图片
     */
    @Override
    public Localpic selectLocalpicById(Long id)
    {
        return localpicMapper.selectLocalpicById(id);
    }

    /**
     * 查询本地图片列表
     * 
     * @param localpic 本地图片
     * @return 本地图片
     */
    @Override
    public List<Localpic> selectLocalpicList(Localpic localpic)
    {
        return localpicMapper.selectLocalpicList(localpic);
    }

    /**
     * 新增本地图片
     * 
     * @param localpic 本地图片
     * @return 结果
     */
    @Override
    public int insertLocalpic(Localpic localpic)
    {
        return localpicMapper.insertLocalpic(localpic);
    }

    /**
     * 修改本地图片
     * 
     * @param localpic 本地图片
     * @return 结果
     */
    @Override
    public int updateLocalpic(Localpic localpic)
    {
        return localpicMapper.updateLocalpic(localpic);
    }

    /**
     * 批量删除本地图片
     * 
     * @param ids 需要删除的本地图片主键
     * @return 结果
     */
    @Override
    public int deleteLocalpicByIds(Long[] ids)
    {
        return localpicMapper.deleteLocalpicByIds(ids);
    }

    /**
     * 删除本地图片信息
     * 
     * @param id 本地图片主键
     * @return 结果
     */
    @Override
    public int deleteLocalpicById(Long id)
    {
        return localpicMapper.deleteLocalpicById(id);
    }

    /***
     * 发送消息到kafka
     * @param ids
     * @return
     */
    @Override
    public int sendtokafka(Long[] ids) {
        for (Long id : ids) {
            // 获取图片的url
            Localpic localpic = selectLocalpicById(id);
            String pic_url = localpic.getPic();

            // 序列化对象
            String jsonString = JSONObject.toJSONString(generateHumanSnap(pic_url));
            CompletableFuture<SendResult<String, Object>> send = kafkaTemplate.send("HIK_SMART_METADATA_TOPIC_LOCAL", jsonString);
            // 给所有数据推送次数+1

            //log.info("1-------{}-------",localpic.getPushCount());
            if (localpic.getPushCount() == null) {localpic.setPushCount(0);}
            //log.info("2-------{}-------",localpic.getPushCount());
            int i = localpic.getPushCount();
            localpic.setPushCount(++i);
            //log.info("3-------{}-------",localpic.getPushCount());
            localpic.setStructuredData(jsonString);
            String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            //log.info("------format---------{}---",format);
            localpic.setConTime(format);
            updateLocalpic(localpic);
        }
        return ids.length;
    }

    /**
     * 把字典的key换成value
     * @param list
     * @return List<Localpic>
     */
    @Override
    public List<Localpic> generateExportData(List<Localpic> list) {
        for (Localpic localpic : list) {
            // 获取图片类型
            String picType = localpic.getPicType();
            String picTypeValue = DictUtils.getDictLabel("manage_pic_type", picType);
            localpic.setPicType(picTypeValue);
        }
        return list;
    }

    public HumanSnap generateHumanSnap(String pic_url){
        HumanSnap humanSnap = new HumanSnap();
        // 构造humanSnap
        humanSnap.setSendTime(new Date());
        humanSnap.setChannelID(1);
        humanSnap.setRecvTime(new Date());
        humanSnap.setEventDescription("hawkResult");
        humanSnap.setEventType("hawkResult");
        humanSnap.setIpAddress("32.73.1.95");
        humanSnap.setPortNo(9304);
        humanSnap.setDataType("human");
        humanSnap.setDateTime(new Date());


        // 构造human
        Human human = new Human();
        human.setBag(new ValueObject("unknown"));
        human.setGender(new ValueObject("unknown"));
        human.setHat(new ValueObject("unknown"));
        human.setGlass(new ValueObject("unknown"));
        human.setHairStyle(new ValueObject("unknown"));
        human.setHatStyle(new ValueObject("unknown"));
        human.setJacketColor(new ValueObject("red"));
        human.setJacketType(new ValueObject("unknown"));
        human.setMask(new ValueObject("unknown"));
        human.setThings(new ValueObject("unknown"));
        human.setTrousersType(new ValueObject("unknown"));
        human.setTrousersColor(new ValueObject("unknown"));

        // 构造rect
        Rect rect = new Rect(0.0,0.0,0.0,0.0);


        // 构造target
        Target target = new Target();
        target.setEndFlag(true);
        target.setGlobalId(UUID.randomUUID().toString());
        target.setHuman(human);
        target.setId(1);
        target.setSegNum(1);
        target.setTargetSubUrl("");
        target.setTargetType(new ValueObject("human"));
        target.setRect(rect);

        // 构造targetAttrs
        TargetAttrs targetAttrs = new TargetAttrs();
        targetAttrs.setBodyTime(new Date());
        targetAttrs.setCameraAddress("牛塘君悦会国际俱乐部入口R细节");
        targetAttrs.setCameraIndexCode("32041222031321300239");
        targetAttrs.setCameraName("牛塘君悦会国际俱乐部入口R细节");
        targetAttrs.setDeviceIndexCode("32041222031321300239");
        targetAttrs.setDeviceName("牛塘君悦会国际俱乐部入口R细节");
        targetAttrs.setDeviceType("100001");
        targetAttrs.setImageServerCode("1c1cea0c-efe0-4bb8-a4db-f119e660c8dc");
        targetAttrs.setLatitude(0.0);
        targetAttrs.setLongitude(0.0);
        targetAttrs.setRecognitionSign(1);
        targetAttrs.setRegionIndexCode("173792f60fde4719a0e0c43c1fe7091e");
        targetAttrs.setTdaGlobalId(UUID.randomUUID().toString());

        // 构造target的list
        ArrayList<Target> targets = new ArrayList<>();
        targets.add(target);

        // 构造analysisResult
        AnalysisResult analysisResult = new AnalysisResult();
        analysisResult.setStreamType("realtime");
        analysisResult.setTargetPicUrl(pic_url);
        analysisResult.setTaskID(UUID.randomUUID().toString());
        analysisResult.setTarget(targets);
        analysisResult.setTargetAttrs(targetAttrs);

        // 创建构造analysisResult的list
        ArrayList<AnalysisResult> analysisResults = new ArrayList<>();
        analysisResults.add(analysisResult);

        // 构造humanSnap
        humanSnap.setAnalysisResult(analysisResults);


        return humanSnap;
    }
}
